import {
  modifyAdd
} from '../../../api.js'
var app = getApp(); //获取App内部的信息
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked: false,
    region: [],
    regions: '',
    radio_btn: false,
    province: '',
    city: '',
    district: '',
    is_default: 0,
    mold: '',
    addname: '',
    addphobe: '',
    addss: '',
    lis_data: [],
    addChoice: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var mold = options.mold;
    var nums = options.addChoice;
    var inde = options.ind
    var that = this
    if (mold == 0) {
      that.setData({
        mold: mold
      })
      if (nums == 0) {
        that.setData({
          addChoice: options.addChoice
        })
      }
      return false;
    } else {
      var sh = JSON.parse(options.toAuth)
      that.setData({
        mold: mold,
        lis_data: sh,
        addname: sh.name,
        addphobe: sh.phone,
        addss: sh.address,
        regions: sh.province + '-' + sh.city + '-' + sh.district,
        province: sh.province,
        city: sh.city,
        district: sh.district,
        addChoice: inde
      })
      if (sh.is_default == 1) {
        that.setData({
          is_default: 1,
          radio_btn: true
        })
      } else {
        that.setData({
          is_default: 0,
          radio_btn: false
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  formSubmit: function (e) {
    var _form = e.detail.value;
    var that = this
    var isPhone = /^1[3|4|5|8][0-9]\d{4,8}$/;
    var isok = isPhone.test(_form.phone);
    if (!_form.name) {
      wx.showToast({
        title: '请填姓名',
        icon: 'none'
      })
      return false;
    } else if (!_form.phone) {
      wx.showToast({
        title: '请填写手机号',
        icon: 'none'
      })
      return false;
    } else if (!_form.address) {
      wx.showToast({
        title: '请填详情地址',
        icon: 'none'
      })
      return false;
    } else if (!that.data.province || !that.data.city || !that.data.district) {
      wx.showToast({
        title: '请选择地址',
        icon: 'none'
      })
      return false;
    } else {
      var mold_num = that.data.mold;
      if (mold_num == 0) {
        modifyAdd({
          token: app.globalData.userInfo.token,
          name: _form.name,
          phone: _form.phone,
          province: that.data.province,
          city: that.data.city,
          district: that.data.district,
          address: _form.address,
          is_default: that.data.is_default
        }).then(res => {
          if (res.data.code == 666) {
            wx.showToast({
              title: "添加成功"
            })
            setTimeout(function () {
              wx.redirectTo({
                url: '/pages/public/myadd/myadd?addChoice=' + that.data.addChoice
              })
            }, 1500)
          } else if (res.data.code == 111111) {
            wx.showToast({
              title: '登录失效，请重新登录',
              icon: 'none',
            })
            setTimeout(function () {
              wx.reLaunch({
                url: '/pages/user/login/login',
              })
            }, 1500)
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
            return false;
          }
        })
      } else {
        var ids = that.data.lis_data.id
        modifyAdd({
          token: app.globalData.userInfo.token,
          id: ids,
          name: _form.name,
          phone: _form.phone,
          province: that.data.province,
          city: that.data.city,
          district: that.data.district,
          address: _form.address,
          is_default: that.data.is_default
        }).then(res => {
          if (res.data.code == 666) {
            var pages = getCurrentPages();
            var prevPage = pages[pages.length - 2];   //当前页面
            console.log(prevPage.data.lis)
            wx.showToast({
              title: "修改成功"
            })
            setTimeout(function () {
              wx.redirectTo({
                url: '/pages/public/myadd/myadd?addChoice=' + that.data.addChoice
              })
            }, 1500)
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
            return false;
          }
        })
      }
    }
  },
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value,
      regions: e.detail.value[0] + '-' + e.detail.value[1] + '-' + e.detail.value[2],
      province: e.detail.value[0],
      city: e.detail.value[1],
      district: e.detail.value[2]
    })
  },
  radio: function () {
    var typ = this.data.radio_btn;
    var that = this;
    that.setData({
      radio_btn: !typ
    })
    if (that.data.radio_btn == true) {
      that.setData({
        is_default: 1
      })
    } else {
      that.setData({
        is_default: 0
      })
    }
  },
  upadd: function () {
    var that = this
    if (wx.chooseAddress) {
      wx.chooseAddress({
        success: function (res) {
          console.log(res)
          that.setData({
            addname: res.userName,
            addphobe: res.telNumber,
            regions: res.provinceName + '-' + res.cityName + '-' + res.countyName,
            addss: res.detailInfo,
            radio_btn: true,
            province: res.provinceName,
            city: res.cityName,
            district: res.countyName
          })
        },
        fail: function (err) {
          console.log(JSON.stringify(err))
        }
      })
    } else {
      console.log('当前微信版本不支持自动识别地址');
    }
  }
})